# Maintainer: Yuta Nakai <nak5124@live.jp>

_realname=dcadec
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgdesc='dcadec is a free DTS Coherent Acoustics decoder with support for HD extensions (mingw-w64)'
pkgver=0.2.0
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://github.com/foo86/dcadec'
license=('LGPL')
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/foo86/dcadec/archive/v${pkgver}.tar.gz"
        '0001-Makefile-Don-t-set-static-libgcc-on-mingw-systems.patch'
        '0002-Makefile-Install-windows-DLL-to-BINDIR.patch'
        '0003-Makefile-On-windows-install-import-library.patch'
        '0004-Makefile-Add-API-version-suffix-to-DLL-Name.patch'
        '0005-Makefile-Add-install-cut.patch')
sha512sums=('94edcc0b4449882ebeb08f8b39fe44a5302731df2dad3a3e0b0f787fd7c2c3e3992fed95e6f594af516e982cec34b057aab30e6457c098df61f495a487125acb'
            '80469f7ad04d7c247dda9c56d7aa9771c3b6ddd8aa238604be7b325ae7ac87cbdfa8668d2212adfa69ddaad3bdd7cad631f0b226255fb5cb6b432f66f6dc8de3'
            'ec11e3a5c300f709f8660c411aaafc98982100a346ad28242640103c2147aaa6dbefdb2a24e5d9198e29111f95fa18faccfd2394a9677c305028e049b4b9879f'
            'f2f7b99dcaf6f4b9d1ffd2ed0dd6f61c6eaba6ee7425752f428e864cec135fc6a4c6c6cf001bfc598ba4a8a487b85a59cfba93a5f59d71bc4a286400efbea985'
            'd4839f9d9016fe957e9c7b1b6c45fe5b21a1db8f1e2ef96295cb37a59e87c591c8c8681755df4600c7d441d7d1268e621264d5b14cdcb092e33216b44f158287'
            '9ae1446ac65d635858afa05f90734990d08096758bf03a24b9a74d98255643d90badd905e2718eeb97c4a79375c6714585d43c69f47e573d0d7497cc55dcd018')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}"/0001-Makefile-Don-t-set-static-libgcc-on-mingw-systems.patch
  patch -p1 -i "${srcdir}"/0002-Makefile-Install-windows-DLL-to-BINDIR.patch
  patch -p1 -i "${srcdir}"/0003-Makefile-On-windows-install-import-library.patch
  patch -p1 -i "${srcdir}"/0004-Makefile-Add-API-version-suffix-to-DLL-Name.patch
  patch -p1 -i "${srcdir}"/0005-Makefile-Add-install-cut.patch
}

build() {
  # Build shared lib and exe.
  mkdir -p "${srcdir}"/build-shared-$CARCH
  cd "${srcdir}"/build-shared-$CARCH

  make clean -f "${srcdir}/${_realname}-${pkgver}/Makefile"

  PREFIX=$MINGW_PREFIX \
  CONFIG_NDEBUG=1 \
  CONFIG_WINDOWS=1 \
  CONFIG_SHARED=1 \
  CC=gcc \
  make all -f "${srcdir}/${_realname}-${pkgver}/Makefile"

  # Build static lib.
  mkdir -p "${srcdir}"/build-static-$CARCH
  cd "${srcdir}"/build-static-$CARCH

  make clean -f "${srcdir}/${_realname}-${pkgver}/Makefile"

  PREFIX=$MINGW_PREFIX \
  CONFIG_NDEBUG=1 \
  CONFIG_WINDOWS=1 \
  CC=gcc \
  make lib -f "${srcdir}/${_realname}-${pkgver}/Makefile"
}

package() {
  # Install shared lib and exe.
  cd "${srcdir}"/build-shared-$CARCH

  PREFIX=$MINGW_PREFIX \
  CONFIG_NDEBUG=1 \
  CONFIG_WINDOWS=1 \
  CONFIG_SHARED=1 \
  DESTDIR="${pkgdir}" \
  make install install-cut -f "${srcdir}/${_realname}-${pkgver}/Makefile"

  # Install static lib.
  cd "${srcdir}"/build-static-$CARCH

  PREFIX=$MINGW_PREFIX \
  CONFIG_NDEBUG=1 \
  CONFIG_WINDOWS=1 \
  DESTDIR="${pkgdir}" \
  make install-lib -f "${srcdir}/${_realname}-${pkgver}/Makefile"

  # Install LICENSE file.
  cd "${srcdir}/${_realname}-${pkgver}"
  install -Dm644 COPYING.LGPLv2.1 "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  # Rename dcadec executable to not conflict with libdca
  mv ${pkgdir}${MINGW_PREFIX}/bin/dcadec{,0}.exe
}
